package com.toommi.refuse.controller;

import com.toommi.refuse.annotation.PermissionController;
import com.toommi.refuse.annotation.PermissionReport;
import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.entity.RefuseType;
import com.toommi.refuse.exception.RefuseTypeException;
import com.toommi.refuse.service.RefuseTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * ..
 * 分类维护
 *
 * @author yangg
 * @since 2019-07-26 10:19
 */
@RestController
@RequestMapping(value = "/api/refuse")
@Slf4j
@AllArgsConstructor
@PermissionController(groupCode = "admin_refusetype", groupName = "分类管理")
@Api(tags = "大件垃圾分类管理")
public class RefuseTypeController {

    private RefuseTypeService typeService;

    /**
     * 新增类型
     *
     * @param refuseType 类型信息
     * @return 操作结果
     */
    @ApiOperation(value = "新增大件垃圾分类", notes = "新增大件垃圾分类")
    @ApiImplicitParam(name = "refuseType", value = "分类信息实体", required = true, dataType = "RefuseType")
    @RequiresPermissions("admin:refusetype:add")
    @PermissionReport(value = "admin:refusetype:add", name = "新增分类", group = "admin_refusetype")
    @RequestMapping(value = "/type", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public CommonResponse addType(@RequestBody RefuseType refuseType) {
        CommonResponse commonResponse = CommonResponse.success();

        try {
            typeService.addType(refuseType);
        } catch (RefuseTypeException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add refuse type error :{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 删除指定的分类信息
     *
     * @param id 分类信息id
     * @return 删除结果
     */
    @ApiOperation(value = "删除大件垃圾分类", notes = "删除大件垃圾分类")
    @ApiImplicitParam(name = "id", value = "分类信息id", required = true, dataType = "Long")
    @RequiresPermissions("admin:refusetype:delete")
    @PermissionReport(value = "admin:refusetype:delete", name = "删除分类", group = "admin_refusetype")
    @RequestMapping(value = "/type/{id}", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8")
    public CommonResponse deleteType(@PathVariable(name = "id") long id) {
        CommonResponse commonResponse = CommonResponse.success();
        try {
            typeService.deleteType(id);
        } catch (Exception e) {
            log.error("delete refuse type error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 根据大类查找分类信息
     *
     * @param code 大类code
     * @return 分类信息
     */
    @ApiOperation(value = "查询大件垃圾分类", notes = "查询大件垃圾分类")
    @ApiImplicitParam(name = "code", value = "垃圾分类大类，大件垃圾传入large", required = true, dataType = "String")
    @RequestMapping(value = "/types", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public CommonResponse getTypes(@RequestParam String code) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(typeService.findTypes(code));
        } catch (Exception e) {
            log.error("get refuse type error :{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


    /**
     * 获取所有分类
     *
     * @return 分类信息
     */
    @ApiOperation(value = "查询大件垃圾分类", notes = "查询大件垃圾分类")
    @RequestMapping(value = "/alltype")
    public CommonResponse getTypes() {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(typeService.getTypeNameList());
        } catch (Exception e) {
            log.error("get refuse type error :{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


    /**
     * 修改分类金额
     *
     * @param
     * @return 分类信息
     */
    @ApiOperation(value = "修改分类金额", notes = "修改分类金额")

    @RequestMapping(value = "/updatejfgz", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public CommonResponse updatejfgz(HttpServletRequest request) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            typeService.updatejfgz(request);
        } catch (Exception e) {
            log.error("get refuse type error :{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


    /**
     * 根据分类ID获取小分类
     *
     * @param id 大类id
     * @return 分类信息
     */
    @ApiOperation(value = "根据分类ID获取小分类", notes = "根据分类ID获取小分类")
    @ApiImplicitParam(name = "id", value = "根据分类ID获取小分类", required = true, dataType = "String")
    @RequestMapping(value = "/typesinfo", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public CommonResponse getTypesInfo(@RequestParam String id) {
        CommonResponse commonResponse = CommonResponse.success();
        try {
            commonResponse.setData(typeService.getTypesInfo(id));
        } catch (Exception e) {
            log.error("get refuse type error :{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 功能描述: 获取所有分类列表
     *
     * @param: []
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/13 0013 上午 7:57
     */
    @GetMapping("/typelist")
    public Object getTypeList(int page, int limit) {
        try {
            Object object = typeService.getTypeList(page, limit);
            return object;
        } catch (Exception e) {
            log.error("get refuse type list error :{}", e);
            return null;
        }
    }

    /**
     * 功能描述: 根据id获取分类信息
     *
     * @param: [id]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/15 0015 上午 10:40
     */
    @GetMapping("/typeinfo")
    public CommonResponse getRefuseTypeById(Integer id) {
        CommonResponse response = CommonResponse.success();
        try {
            RefuseType refuseType = typeService.findOneById(id);
            if (refuseType == null) {
                response.setCode(404);
                response.setMessage("未查询到分类信息");
            } else {
                response.setData(refuseType);
            }

            return response;
        } catch (Exception e) {
            log.error("get refuse type error:{}", e);
            response.setMessage("获取分类信息出现错误");
            response = CommonResponse.serverError();
            return response;
        }
    }


    /**
     * 功能描述: 根据id修改分类信息
     *
     * @param: [refusetype]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/16 0016 上午 9:59
     */
    @PostMapping(value = "/updatetype", produces = "application/json;charset=UTF-8")
    public CommonResponse updateRefuseTypeById(@RequestBody RefuseType refusetype) {
        CommonResponse response = CommonResponse.success();
        try {
            typeService.updateTypeById(refusetype);
            return response;
        } catch (Exception e) {
            log.error("update refuse type error:{}", e);
            response = CommonResponse.serverError();
            return response;
        }
    }

    /**
     * 功能描述: 新增垃圾分类
     *
     * @param: [refuseType]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/16 0016 上午 10:16
     */
    @RequestMapping(value = "/addtype", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public CommonResponse addRefuseType(@RequestBody RefuseType refuseType) {
        CommonResponse commonResponse = CommonResponse.success();
        try {
            typeService.addRefuseType(refuseType);
        } catch (RefuseTypeException e) {
            commonResponse.setCode(e.getCode());
            commonResponse.setMessage(e.getMessage());
        } catch (Exception e) {
            log.error("add refuse type error :{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }


    /**
     * 功能描述: 根据id删除垃圾分类
     *
     * @param: [id]
     * @return: com.toommi.refuse.common.response.CommonResponse
     * @auther: HuangBo
     * @date: 2019/9/16 0016 下午 13:00
     */
    @GetMapping("/deltype")
    public CommonResponse delRefuseType(Integer id) {
        CommonResponse response = CommonResponse.success();
        try {
            RefuseType refuseType = typeService.findOneById(id);
            if (refuseType == null) {
                response.setCode(404);
                response.setMessage("不存在的分类");
            } else {
                int res = typeService.deleteTypeById(id);
                if (res < 0) {
                    response.setMessage("删除垃圾分类失败");
                }
            }
            return response;
        } catch (Exception e) {
            log.error("delete refuse type eror:{}", e);
            response.setMessage("删除垃圾分类出现异常");
            return response;
        }
    }

    /**
     * 功能描述: 根据条件获取垃圾分类信息
     *
     * @param: [page, limit, type]
     * @return: java.lang.Object
     * @auther: HuangBo
     * @date: 2019/9/16 0016 下午 13:05
     */
    @GetMapping("/typecondition")
    public Object findTypeByCondition(int page, int limit, @RequestParam(required = false) String type) {
        try {
            Object obj = typeService.findTypesByCondition(page, limit, type);
            return obj;
        } catch (Exception e) {
            log.error("get type list by condition error:{}", e);
            return null;
        }
    }
}
